/**
 * 
 */
package template.page.admin;

import java.util.ArrayList;
import java.util.List;

import org.apache.click.control.HiddenField;
import org.apache.click.control.Option;
import org.apache.click.control.Submit;
import org.apache.click.dataprovider.DataProvider;
import org.apache.click.extras.control.PickList;
import org.apache.click.util.Bindable;

import template.page.user.UserDetail;
import cvcg.model.Game;
import cvcg.model.Player;
import cvcg.model.Role;



/**
 * @author jaracje
 *
 */
@SuppressWarnings("serial")
public class EditPlayer extends UserDetail {

	private PickList rolesPickList = new PickList("rolesPickList","Roles");
	private HiddenField idField = new HiddenField("id",Long.class);
	@Bindable protected Long id;
	
	public EditPlayer() 
	{		
		super();
		nameField.setReadonly(false);		
		nameField.setRequired(true);
		playerForm.add(idField);
		playerForm.add(new Submit("ok", " Ok ", this, "onOkClicked"));
		playerForm.add(new Submit("cancel", " Cancel ", this, "onCancelClicked"));
		playerForm.remove(saveSubmit);
		detailFieldSet.add(rolesPickList);
	}
	
    @Override
    public void onGet() 
    {
        if (id != null) {
            Player player = gameSessionService.getPlayerById(id);
            if (player != null) 
            {
                // Copy customer data to form. The idField value will be set by
                // this call
            	playerForm.copyFrom(player);            	
            	title = title + " " + player.getName();
            	
            	for(Game game : player.getOwnedGames()) 
            	{
            		if (game.getId()!=null) 
            		{
            			ownedGamesPickList.addSelectedValue(game.getId().toString());
            		}
            	}
            	for(Game game : player.getRuleMasteredGames()) 
            	{
            		if (game.getId()!=null) 
            		{
            			ruleMasteredGamesPickList.addSelectedValue(game.getId().toString());
            		}
            	}     
            	for(Role role : player.getRoles()) 
            	{
            		rolesPickList.addSelectedValue(role.getRoleName());
            	}
            }
        }
    }

	/**
     * Handle the OK button click event.
     *
     * @return true
     */
    public boolean onOkClicked() 
    {
        if (playerForm.isValid()) 
        {
        	Long idValue = (Long) idField.getValueObject();
        	gameSessionService.savePlayer(idValue, playerForm, ownedGamesPickList, ruleMasteredGamesPickList,rolesPickList);
        	setRedirect(ManagePlayers.class);
        	return true;
        }        
        return false;
    }
    
    /**
     * Handle the Cancel button click event.
     *
     * @return false
     */
    public boolean onCancelClicked() 
    {
    	setRedirect(ManagePlayers.class);
        return true;
    }

	@SuppressWarnings({"rawtypes"})
	@Override
	public void onInit() 
	{
		super.onInit();
		DataProvider rolePickListDataProvider = new DataProvider() 
		{
			public List getData() 
			{
				List<Role> roleList = gameSessionService.getAllRoles();
		        List<Option> roleOptions = new ArrayList<Option>();
		        
		        for (Role role : roleList) {
		        	roleOptions.add(new Option(role.getRoleName(),role.getRoleName()));
				}
				return roleOptions;
			}
		};
		rolesPickList.setDataProvider(rolePickListDataProvider);
	}
}